package vue;

import java.awt.Dimension;
import java.util.Random;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.util.concurrent.*;

public class Duck extends JLabel { 
	// Variable(s) statique(s)
	private static final long serialVersionUID = 4722436041732650683L;
	
	// Variable(s) d'instance
	private int x;
	private int y;
	private int signe;
	private int signeY;
	private int coupsRestants;
	private int adresseImage;
	private int compteurAiles;
	private boolean isDead;
	private Semaphore sem;
	
	// Constructeur(s)
	public Duck(int x, int y) {
		super();
		
		this.sem = new Semaphore(0);
		this.isDead=false;
		this.compteurAiles=0;
		this.x = x;
		this.y = y;
		this.coupsRestants=3;
		if(x == 0) {
			this.signe = 1;
			this.adresseImage=1;
		} else {
			this.signe = 0;
			this.adresseImage=12;
		}
		Random r = new Random();
		int signe = 1 + r.nextInt(3 - 1);
		if(signe == 1) {
			this.signeY=0;
		} else {
			this.signeY=1;
		}
		Icon icon = new ImageIcon("./images/canards/"+this.adresseImage+".png");
	    this.setIcon(icon);
	    this.setBounds(this.x, this.y, 96, 72);
	}
	
	// Accesseur(s)
	public int getX() {
		return this.x;
	}
	
	public int getY() {
		return this.y;
	}
	
	public int getSigne() {
		return this.signe;
	}
	
	public int getSigneY() {
		return this.signeY;
	}
	
	public int getCoupsRestants() {
		return this.coupsRestants;
	}
	
	public boolean isDead() {
		return this.isDead;
	}
	
	public Semaphore getSemaphore() {
		return this.sem;
	}
	
	// Mutateur(s)
	public void setX(int x) {
		this.x=x;
		if(this.x < 0) {
			this.x = 0;
			this.setVisible(false);
		}
		Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		if(this.x > (int)dimension.getWidth()-68) {
			this.x = (int)dimension.getWidth()-68;
			this.setVisible(false);
		}
	}
	
	public void setY(int y) {
		this.y=y;
		if(this.y < 0) {
			this.y = 0;
			this.signeY=1;
		}
		Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		if(this.y > (int)dimension.getHeight()-121) {
			this.y = (int)dimension.getHeight()-121;
			this.signeY=0;
			if(this.isDead) {
				this.sem.release();
			}
		}
	}
	
	public void decrementerCoupsRestants() {
		this.coupsRestants--;
	}
	
	public void gererAiles() {
		Icon icon;
		if(this.compteurAiles%13==0) {
			switch(this.adresseImage) {
				case 1 :
					icon = new ImageIcon("./images/canards/"+2+".png");
					this.adresseImage=2;
				    this.setIcon(icon);
					break;
					
				case 2 :
					icon = new ImageIcon("./images/canards/"+3+".png");
					this.adresseImage=3;
				    this.setIcon(icon);
					break;
					
				case 3 :
					icon = new ImageIcon("./images/canards/"+1+".png");
					this.adresseImage=1;
				    this.setIcon(icon);
					break;
					
				case 12 :
					icon = new ImageIcon("./images/canards/"+22+".png");
					this.adresseImage=22;
				    this.setIcon(icon);
					break;
					
				case 22 :
					icon = new ImageIcon("./images/canards/"+32+".png");
					this.adresseImage=32;
				    this.setIcon(icon);
					break;
					
				case 32 :
					icon = new ImageIcon("./images/canards/"+12+".png");
					this.adresseImage=12;
				    this.setIcon(icon);
					break;
			}
		}
		this.compteurAiles++;
	}
	
	public void die() {
		ImageIcon icon;
		this.isDead=true;
		if(this.adresseImage > 10) {
			icon = new ImageIcon("./images/canards/"+42+".png");
			this.adresseImage=42;
			this.setIcon(icon);
		} else {
			icon = new ImageIcon("./images/canards/"+4+".png");
			this.adresseImage=4;
			this.setIcon(icon);
		}
	}
}
